package com.travel.server.manager.impl.command.project;

import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.server.manager.dto.request.project.QueryProjectBaseInfoRequest;
import com.travel.server.manager.dto.response.project.QueryProjectBaseInfoResponse;
import com.travel.server.manager.dto.response.project.QueryProjectBaseInfoResponse.SpecialItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.dal.QuestionnaireDal;
import com.travel.server.manager.impl.dal.SpecialDal;
import com.travel.server.manager.impl.dal.SpecialDal.SpecialParam;
import com.travel.server.manager.model.ProjectInfo;
import com.travel.server.manager.model.ProjectSpecial;
import com.travel.server.manager.model.QuestionnaireInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/31 10:37
 * @Desc:
 */
@Component
@Slf4j
public class QueryProjectBaseInfoCommand extends AbstractCommand<QueryProjectBaseInfoRequest, QueryProjectBaseInfoResponse> {

    @Autowired
    private ProjectDal projectDal;

    @Autowired
    private SpecialDal specialDal;

    @Autowired
    private QuestionnaireDal questionnaireDal;

    @Override
    protected void checkInput(QueryProjectBaseInfoRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryProjectBaseInfoResponse handle(QueryProjectBaseInfoRequest request) {
        checkInput(request);
        //查项目信息
        ProjectInfo projectInfo = projectDal.selectById(request.getId());
        if(projectInfo == null){
            throw new WorkException(WorkCode.RECORD_NOT_EXIST);
        }
        //项目规格
        SpecialParam specialParam = new SpecialParam();
        List<Integer> projectIdList = new ArrayList<>(Arrays.asList(projectInfo.getId()));
        specialParam.setProjectIdList(projectIdList);
        List<ProjectSpecial> specialList = specialDal.selectByExample(specialParam);

        QueryProjectBaseInfoResponse response = new QueryProjectBaseInfoResponse();
        response.setCountry(projectInfo.getCountry());
        response.setEmails(projectInfo.getEmails());
        response.setExplains(projectInfo.getProjectExplains());
        response.setLimitNumber(projectInfo.getLimitNumber() == null ? CommonConstant.EMPTY_STR : projectInfo.getLimitNumber() + CommonConstant.EMPTY_STR);
        if(projectInfo.getLinePrice() != null) {
            response.setLinePrice(CommonUtil.fenToYuan(projectInfo.getLinePrice()).toString());
        }
        if(projectInfo.getMemberPrice() != null) {
            response.setMemberPrice(CommonUtil.fenToYuan(projectInfo.getMemberPrice()).toString());
        }
        response.setMemberType(projectInfo.getMemberType());
        response.setName(projectInfo.getName());
        response.setNeedEnglish(projectInfo.getNeedEnglish());
        response.setNumber(projectInfo.getNumber());
        response.setOrderChargeId(projectInfo.getOrderChargeId());
        response.setOrderChargeName(projectInfo.getOrderChargeName());
        if(projectInfo.getPrice() != null) {
            response.setPrice(CommonUtil.fenToYuan(projectInfo.getPrice()).toString());
        }
        response.setPriUrl(projectInfo.getProjectPicUrl());
        response.setProject(projectInfo.getProjectGroup());
        response.setProjectChargeId(projectInfo.getProjectChargeId());
        response.setProjectChargeName(projectInfo.getProjectChargeName());
        if(projectInfo.getQuestionId() != null) {
            QuestionnaireInfo question = questionnaireDal.selectById(projectInfo.getQuestionId());
            response.setQuestion(question.getQuestionnaireName());
            response.setQuestionId(question.getId());
        }
        response.setShelfTime(projectInfo.getShelfTime());
        response.setShelfType(projectInfo.getShelfType());
        response.setStock(projectInfo.getStock() == null ? CommonConstant.EMPTY_STR : projectInfo.getStock() + CommonConstant.EMPTY_STR);
        response.setTravelType(projectInfo.getTravelType());
        if(StringUtils.isNotBlank(projectInfo.getTravelType())){
            QuestionnaireInfo question = questionnaireDal.selectById(Integer.parseInt(projectInfo.getTravelType()));
            response.setTravelType(question.getQuestionnaireName());
            response.setTravelTypeId(question.getId());
        }
        response.setType(projectInfo.getProjectType());
        List<SpecialItem> specialItemList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(specialList)){
            for(ProjectSpecial special : specialList){
                SpecialItem item = new SpecialItem();
                item.setAttention(special.getAttention());
                item.setDays(special.getDays());
                if(special.getPrice() != null) {
                    item.setPrice(CommonUtil.fenToYuan(special.getPrice()).toString());
                }
                item.setSalesVolume(special.getSalesVolume() == null ? CommonConstant.EMPTY_STR : special.getSalesVolume() + CommonConstant.EMPTY_STR);
                item.setSpecialId(special.getId());
                item.setStock(special.getStock() == null ? CommonConstant.EMPTY_STR : special.getStock() + CommonConstant.EMPTY_STR);
                item.setTravelDate(special.getTravelDate());
                item.setApplyAmount(special.getApplyAmount() == null ? CommonConstant.EMPTY_STR : special.getApplyAmount() + CommonConstant.EMPTY_STR);
                specialItemList.add(item);
            }
        }
        response.setSpecialList(specialItemList);
        return response;
    }
}
